package com.sansan.qiangji.realize.base;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sansan.qiangji.VO.base.device.DeviceListResultVO;
import com.sansan.qiangji.VO.base.device.DeviceVO;
import com.sansan.qiangji.entity.base.Device;
import com.sansan.qiangji.entity.base.Device;
import com.sansan.qiangji.entity.base.Device;
import com.sansan.qiangji.entity.base.example.DeviceExample;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.base.DeviceAddForm;
import com.sansan.qiangji.form.base.DeviceEditForm;
import com.sansan.qiangji.form.base.DeviceListForm;
import com.sansan.qiangji.service.inter.base.DeviceService;
import com.sansan.qiangji.utils.CopyUtil;
import com.sansan.qiangji.utils.pageUtils.PageResult;
import com.sansan.qiangji.utils.pageUtils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DeviceReal {
    @Autowired
    private DeviceService deviceService;

    @Transactional
    public void deleteDevice(Integer deviceId){
        Device device = deviceService.selectByPrimaryKey(deviceId);
        if(device==null){
            log.error("【设备删除】设备不存在，deviceId={}",deviceId);
            throw new ServiceException(ResultEnum.DEVICE_IS_NOT_EXIST);
        }
        int row = deviceService.deleteByPrimaryKey(deviceId);
        if(row==0){
            log.error("【设备删除】设备删除失败，deviceId={}",deviceId);
            throw new ServiceException(ResultEnum.DEVICE_DELETE_ERROR);
        }
    }

    @Transactional
    public void addDevice(DeviceAddForm deviceAddForm){
        DeviceExample deviceExample = new DeviceExample();
        deviceExample.or().andDeviceCodeEqualTo(deviceAddForm.getDeviceCode());
        List<Device> deviceList = deviceService.selectByExample(deviceExample);
        if(deviceList.size()>0){
            log.error("【设备新增】该设备名称或编号已存在，deviceAddForm={}",deviceAddForm.toString());
            throw new ServiceException(ResultEnum.DEVICE_IS_EXIST);
        }
        Device device = new Device();
        BeanUtils.copyProperties(deviceAddForm,device);
        device.setCreateTime(new Date());
        int row = deviceService.insert(device);
        if(row==0){
            log.error("【设备新增】设备新增失败");
            throw new ServiceException(ResultEnum.DEVICE_INSERT_ERROR);
        }
    }



    @Transactional
    public void editDevice(DeviceEditForm deviceEditForm){
        Device device = deviceService.selectByPrimaryKey(deviceEditForm.getId());
        if (device == null){
            log.error("【编辑物品】物品不存在，deviceEditForm={}",deviceEditForm.toString());
            throw new ServiceException(ResultEnum.DEVICE_IS_NOT_EXIST);
        }
        CopyUtil.copyPropertiesNotNull(deviceEditForm,device);
        int row = deviceService.updateByPrimaryKey(device);
        if(row==0){
            log.error("【物品新增】物品新增失败");
            throw new ServiceException(ResultEnum.DEVICE_INSERT_ERROR);
        }
    }


    public DeviceListResultVO deviceList(DeviceListForm deviceListForm){
        DeviceExample deviceExample = new DeviceExample();
        int pageNum = deviceListForm.getPageNum();
        int pageSize = deviceListForm.getPageSize();
        PageHelper.startPage(pageNum,pageSize);
        DeviceExample.Criteria criteria = deviceExample.createCriteria();
        if(deviceListForm.getDeviceCode()!=null&&!"".equals(deviceListForm.getDeviceCode())){
            criteria.andDeviceCodeLike("%"+deviceListForm.getDeviceCode()+"%");
        }
        if(deviceListForm.getDeviceName()!=null&&!"".equals(deviceListForm.getDeviceName())){
            criteria.andDeviceNameLike("%"+deviceListForm.getDeviceName()+"%");
        }
        if(deviceListForm.getDeviceType()!=null&&!"".equals(deviceListForm.getDeviceType())){
            criteria.andDeviceTypeLike("%"+deviceListForm.getDeviceType()+"%");
        }
        List<Device> deviceList = deviceService.selectByExample(deviceExample);
        PageResult<Device> devicePageResult = PageUtil.getPageResult(new PageInfo<>(deviceList));
        DeviceListResultVO deviceListResultVO = new DeviceListResultVO();
        BeanUtils.copyProperties(devicePageResult,deviceListResultVO);
        List<DeviceVO> deviceVOList = new ArrayList<>();
        for(Device device : devicePageResult.getContent()){
            DeviceVO deviceVO = new DeviceVO();
            BeanUtils.copyProperties(device,deviceVO);
            deviceVOList.add(deviceVO);
        }
        deviceListResultVO.setDeviceVOS(deviceVOList);
        return deviceListResultVO;
    }
}
